#include "dialogpreferences.h"
#include "ui_dialogpreferences.h"
#include <QSignalMapper>
#include <QColorDialog>

extern QString COLOR_STYLE;


DialogPreferences::DialogPreferences(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogPreferences),
    m_mapper(new QSignalMapper(this))
{
    ui->setupUi(this);
    connect(ui->pushButtonAttrV,SIGNAL(clicked()),m_mapper,SLOT(map()));
    connect(ui->pushButtonChar,SIGNAL(clicked()),m_mapper,SLOT(map()));
    connect(ui->pushButtonComment,SIGNAL(clicked()),m_mapper,SLOT(map()));
    connect(ui->pushButtonElement,SIGNAL(clicked()),m_mapper,SLOT(map()));
    connect(ui->pushButtonError,SIGNAL(clicked()),m_mapper,SLOT(map()));
    connect(ui->pushButtonName,SIGNAL(clicked()),m_mapper,SLOT(map()));
    connect(ui->pushButtonOther,SIGNAL(clicked()),m_mapper,SLOT(map()));
    connect(m_mapper,SIGNAL(mapped(int)),SLOT(showColorDialog(int)));

    m_mapper->setMapping(ui->pushButtonAttrV,AttributeName);
    m_mapper->setMapping(ui->pushButtonChar,SyntaxChar);
    m_mapper->setMapping(ui->pushButtonComment,Comment);
    m_mapper->setMapping(ui->pushButtonElement,ElementName);
    m_mapper->setMapping(ui->pushButtonError,Error);
    m_mapper->setMapping(ui->pushButtonName,AttributeName);
    m_mapper->setMapping(ui->pushButtonOther,Other);
}

DialogPreferences::~DialogPreferences()
{
    delete ui;
}

void DialogPreferences::setColors(const QMap<HighlightType, QColor> &map)
{
    this->map = map;
    QColor c = map[SyntaxChar];
    ui->pushButtonChar->setStyleSheet(COLOR_STYLE.arg(c.name()).arg(XmlHighlighter::getIdealTextColor(c).name()));

    c = map[ElementName];
    ui->pushButtonElement->setStyleSheet(COLOR_STYLE.arg(c.name()).arg(XmlHighlighter::getIdealTextColor(c).name()));

    c = map[Comment];
    ui->pushButtonComment->setStyleSheet(COLOR_STYLE.arg(c.name()).arg(XmlHighlighter::getIdealTextColor(c).name()));

    c = map[AttributeName];
    ui->pushButtonName->setStyleSheet(COLOR_STYLE.arg(c.name()).arg(XmlHighlighter::getIdealTextColor(c).name()));

    c = map[AttributeValue];
    ui->pushButtonAttrV->setStyleSheet(COLOR_STYLE.arg(c.name()).arg(XmlHighlighter::getIdealTextColor(c).name()));

    c = map[Error];
    ui->pushButtonError->setStyleSheet(COLOR_STYLE.arg(c.name()).arg(XmlHighlighter::getIdealTextColor(c).name()));

    c = map[Other];
    ui->pushButtonOther->setStyleSheet(COLOR_STYLE.arg(c.name()).arg(XmlHighlighter::getIdealTextColor(c).name()));
}

void DialogPreferences::setColor(QPushButton *btn, const QColor &color)
{
    btn->setStyleSheet(COLOR_STYLE.arg(color.name()).arg(XmlHighlighter::getIdealTextColor(color).name()));
}

QMap<HighlightType, QColor> DialogPreferences::getColors()
{
    return map;
}

void DialogPreferences::showColorDialog(int id)
{
    QPushButton *btn;
    QColor cColor;

    switch (id)
    {
        case SyntaxChar:
            btn = ui->pushButtonChar;
            cColor = map[SyntaxChar];
            break;
        case ElementName:
            btn = ui->pushButtonElement;
            cColor = map[ElementName];
            break;
        case Comment:
            btn = ui->pushButtonComment;
            cColor = map[Comment];
            break;
        case AttributeName:
            btn = ui->pushButtonName;
            cColor = map[AttributeName];
            break;
        case AttributeValue:
            btn = ui->pushButtonAttrV;
            cColor = map[AttributeValue];
            break;
        case Error:
            btn = ui->pushButtonError;
            cColor = map[Error];
            break;
        case Other:
            btn = ui->pushButtonOther;
            cColor = map[Other];
            break;
    default:
            btn = NULL;
            break;
    }
    if(btn){
        QColor color = QColorDialog::getColor(cColor,this);
        setColor(btn,color);
        HighlightType idx = (HighlightType)id;
        map[idx] = cColor;

    }
}
